﻿using PetaPoco;

namespace Qy.Common
{
    [AppService]
    public class PetaPocoContext : PetaPocoDbContext
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="configuration"></param>
        public PetaPocoContext()
            : base(new PetaPocoDbContextOptions
            {
                // ConnectionString = configuration.GetConnectionString("DbConnection"), 
                ConnectionString = AppSettings.App(new string[] { "PetaPoco", "ConnectionString" }),
                ProviderName = AppSettings.App(new string[] { "PetaPoco", "ProviderName" })
            })
        {
        }
    }


    /// <summary>
    /// PetaPoco数据库上下文
    /// </summary>
    public abstract class PetaPocoDbContext : Database
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="optionsAccessor"></param>
        protected PetaPocoDbContext(PetaPocoDbContextOptions optionsAccessor)
            : base(optionsAccessor.ConnectionString, optionsAccessor.ProviderName)
        {
            KeepConnectionAlive = false; //是否保持长连接
        }
    }

    public class PetaPocoDbContextOptions
    {
        public string ConnectionString { get; set; }
        public string ProviderName { get; set; }
    }


}
